package com.cosina.game.crystallight.model.enemy;

import android.graphics.Point;

import com.cosina.game.crystallight.engine.BattleEngine;
import com.cosina.game.crystallight.engine.util.Time;
import com.cosina.game.crystallight.engine.util.UpCount;
import com.cosina.game.crystallight.model.battle.BattleField;
import com.cosina.game.crystallight.ui.monster.MonsterResource;
import com.cosina.game.crystallight.util.World;

public class Enemy extends Life implements Time, Comparable<Enemy>{
	public static int[] direction;
	public static final int STATUS_STEADY = 0;
	public static final int STATUS_RUNNING = 1;
	public static final int STATUS_DEADING = 2;

	// 1 - 15;
	public Point center;
	private UpCount moveCount;
	public int staus = STATUS_STEADY;
	private int currentPathIndex;
	public int wait = 0;
	private final BattleField field;
	public final UpCount frameCycle;
	public Point lastMove;
	public final int attack;
	
	private int moveDistance = 0;
	
	
	public Enemy(int kind ,BattleField field,int hp,int maneHave,int attack){
		super(kind,hp,maneHave);
		this.attack = attack;
		this.field = field;
		this.frameCycle = new UpCount(MonsterResource.frameInfo[kind][2]);
		initToStart();
	}

	
	public void initToStart(){
		this.currentPathIndex = 0;
		this.lastMove = new Point();
		this.moveCount = new UpCount(44);
		this.center = new Point(field.enemyDoorLocation().x*44 + 22,
				field.enemyDoorLocation().y*44 + 22);
	}
	
	public void hit(Ps ps){
		BattleEngine.psManager.createHit(center.x,center.y);
		hp-= ps.attack;
	}

	@Override
	public void withTimeGo() {
		if(staus == STATUS_STEADY){
			if(wait == 0){
				staus = STATUS_RUNNING;
			}else{
				wait --;
			}
		}
		
		if(staus == STATUS_RUNNING){
			switch (direction[currentPathIndex]) {
			case World.LEFT:
				lastMove.set(-speed, 0);
				center.offset(-speed, 0);
				break;
			case World.RIGHT:
				lastMove.set(speed, 0);
				center.offset(speed, 0);
				break;
			case World.TOP:
				lastMove.set(0, -speed);
				center.offset(0, -speed);
				break;
			case World.BOTTOM:
				lastMove.set(0, speed);
				center.offset(0, speed);
				break;
			}
			
			moveDistance += speed;
			
			if(moveCount.upToAlram(speed)){
				currentPathIndex++;
			}
			frameCycle.upToAlram();
		}
		
	}
	
	public int getFrameIndex(){
		return frameCycle.value;
	}
	
	public boolean isWin() {
		return currentPathIndex == direction.length;
	}

	public int getDirection() {
		return direction[currentPathIndex];
	}


	@Override
	public int compareTo(Enemy another) {
		if(this == another)
			return 0;
		if(this.moveDistance >= another.moveDistance)
			return -1;
		
		return 1;
	}
}
